var api = require("../../api.js"),
    utils = require("../../utils.js"),
    images = require('../../utils/images.js'),
    app = getApp(), timer = 1;
Page({
    data: {
        tab: 1,
        sort: 1,
        miaosha: null,
        coupon_list: [],
        get_coupon_list: [],
        bargain: [],
        mch_id: null,
      typeList: [{ 'image': images.nearbyShop.storeService, 'title': '在线客服' },
        { 'image': images.nearbyShop.storeTel, 'title': '一键拨号' },
        { 'image': images.nearbyShop.storeScan, 'title': '店铺小程序码' },
        { 'image': images.nearbyShop.storeArea, 'title': '一键导航' }],
    },
    onLoad: function(t) {
        // this.setData({mch_id: t.mch_id});
        app.pageOnLoad(this);
        var a = this;
        if (t.scene) {
            console.log("---scene raw---\x3e", t.scene);
            var o = decodeURIComponent(t.scene);
            o && (o = utils.scene_decode(o), console.log("---scene decode---\x3e", o), o.mch_id && (t.mch_id = o.mch_id))
        }
        a.setData({
            tab: t.tab || 1,
            sort: t.sort || 1,
            mch_id: t.mch_id || !1,
            cat_id: t.cat_id || ""
        }), a.data.mch_id || wx.showModal({
            title: "提示",
            content: "店铺不存在！店铺id为空"
        }), setInterval(function() {
            a.onScroll()
        }, 40), this.getShopData();
    },
    onReady: function() {
        app.pageOnReady(this)
    },
    onShow: function() {
        app.pageOnShow(this)
    },
    onHide: function() {
        app.pageOnHide(this)
    },
    onUnload: function() {
        app.pageOnUnload(this)
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.getGoodsList()
    },
    onShareAppMessage: function() {
        return {
            title: this.data.shop ? this.data.shop.name : "商城首页"
        }
    },
    onScroll: function(t) {
        var o = this;
        wx.createSelectorQuery().selectViewport(".after-navber").scrollOffset(function(t) {
            var a = 2 == o.data.tab ? 136.5333 : 85.3333;
            t.scrollTop >= a ? o.setData({
                fixed: !0
            }) : o.setData({
                fixed: !1
            })
        }).exec()
    },
    getShopData: function() {
        var a = this,
            o = (a.data.current_page || 0) + 1,
            e = "shop_data_mch_id_" + a.data.mch_id,
            t = wx.getStorageSync(e);
        t && a.setData({
            shop: t.shop
        }), wx.showNavigationBarLoading(), a.setData({
            loading: !0
        }), app.request({
            url: api.mch.shop,
            data: {
                mch_id: a.data.mch_id,
                tab: a.data.tab,
                sort: a.data.sort,
                page: o,
                cat_id: a.data.cat_id
            },
            success: function(t) {
                1 != t.code ? 0 == t.code && (a.setData({
                    shop: t.data.shop,
                    coupon_list: t.data.coupon_list,
                    hot_list: t.data.goods_list,
                    goods_list: t.data.goods_list,
                    new_list: t.data.new_list,
                    miaosha: t.data.miaosha,
                    update_list: t.data.update_list,
                    pintuan: t.data.pintuan,
                    bargain: t.data.bargain,
                    current_page: o
                }), wx.setStorageSync(e, t.data)) : wx.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1,
                    success: function(t) {
                        // t.confirm && wx.redirectTo({
                        //     url: "/mch/shop/shop"
                        // })
                    }
                });
                a.miaoshaTimer();
            },
            complete: function() {
                wx.hideNavigationBarLoading(), a.setData({
                    loading: !1
                })
            }
        })
    },
  // 在线客服、一键拨号、店铺小程序码、一键导航
  handleFeed(event) {
    var a = this;
    const type = parseInt(event.currentTarget.dataset.type)
    console.log('type： ' + type)
    if (type === 0) {
      
    } else if (type === 1) {
        wx.makePhoneCall({
            phoneNumber: a.data.shop.tel,
          })
    } else if (type === 2) {
      wx.navigateTo({
        url: '/mch/shop-qrcode/shop-qrcode?mch_id=' + a.data.mch_id,
      })
    } else if (type === 3) {
      
      wx.getSetting({
        success: function (t) {
          t.authSetting["scope.userLocation"] ? a.location(event) : app.getauth({
            content: "需要获取您的地理位置授权，请到小程序设置中打开授权！",
            cancel: !1,
            success: function (t) {
              t.authSetting["scope.userLocation"] && a.location(event);
            }
          });
        }
      });

    }
  },
    closeCouponBox: function(t) {
        this.setData({
            get_coupon_list: ""
        });
    },
    getGoodsList: function() {
        var a = this;
        if (3 != a.data.tab && !a.data.loading && !a.data.no_more) {
            a.setData({
                loading: !0
            });
            var o = (a.data.current_page || 0) + 1;
            app.request({
                url: api.mch.shop,
                data: {
                    mch_id: a.data.mch_id,
                    tab: a.data.tab,
                    sort: a.data.sort,
                    page: o,
                    cat_id: a.data.cat_id
                },
                success: function(t) {
                    0 == t.code && (1 == a.data.tab && (t.data.goods_list && t.data.goods_list.length ? (a.data.hot_list = a.data.hot_list.concat(t.data.goods_list), a.setData({
                        hot_list: a.data.hot_list,
                        current_page: o
                    })) : a.setData({
                        no_more: !0
                      })), 2 == a.data.tab && (t.data.goods_list && t.data.goods_list.length ? (a.data.goods_list = a.data.goods_list.concat(t.data.goods_list), a.setData({
                        goods_list: a.data.goods_list,
                        current_page: o
                    })) : a.setData({
                        no_more: !0
                    })))
                },
                complete: function() {
                    a.setData({
                        loading: !1
                    })
                }
            })
        }
    },
    miaoshaTimer: function() {
        var t = this;
        t.data.miaosha && t.data.miaosha.rest_time && (timer = setInterval(function() {
            0 < t.data.miaosha.rest_time ? (t.data.miaosha.rest_time = t.data.miaosha.rest_time - 1, 
            t.data.miaosha.times = t.getTimesBySecond(t.data.miaosha.rest_time), t.setData({
                miaosha: t.data.miaosha
            })) : clearInterval(timer);
        }, 1e3));
    },
    getTimesBySecond: function(t) {
        if (t = parseInt(t), isNaN(t)) return {
            h: "00",
            m: "00",
            s: "00"
        };
        var a = parseInt(t / 3600), e = parseInt(t % 3600 / 60), i = t % 60;
        return 1 <= a && (a -= 1), {
            h: a < 10 ? "0" + a : "" + a,
            m: e < 10 ? "0" + e : "" + e,
            s: i < 10 ? "0" + i : "" + i
        };
    },
    receive: function(t) {
        var e = this, a = t.currentTarget.dataset.index;
        wx.showLoading({
            title: "领取中",
            mask: !0
        }), e.hideGetCoupon || (e.hideGetCoupon = function(t) {
            var a = t.currentTarget.dataset.url || !1;
            e.setData({
                get_coupon_list: null
            }), a && wx.navigateTo({
                url: a + '?mch_id=' + e.data.mch_id
            });
        }), app.request({
            url: api.coupon.receive,
            data: {
                id: a
            },
            success: function(t) {
                wx.hideLoading(), 0 == t.code ? e.setData({
                    get_coupon_list: t.data.list,
                    coupon_list: t.data.coupon_list
                }) : (wx.showToast({
                    title: t.msg,
                    duration: 2e3
                }), e.setData({
                    coupon_list: t.data.coupon_list
                }));
            }
        });
    },
  location: function (t) {
    var that = this;
    wx.openLocation({
      latitude: parseFloat(that.data.shop.lat),
      longitude: parseFloat(that.data.shop.log),
      name: that.data.shop.name,
      address: that.data.shop.address
    });
  },
  toDail: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.shop.tel,
    })
  }
});